﻿// -----------------------------------------------------------------------
// <copyright file="MovingAverage.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ClearLines.Stocks.Domain
{
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;

   /// <summary>
   /// TODO: Update summary.
   /// </summary>
   public class MovingAverage
   {
      public static double? Compute(StockHistory history, DateTime day, int length)
      {
         var sample = history.History
            .Where(it => it.Day <= day)
            .OrderByDescending(it => it.Day)
            .Take(length);

         if (sample.Count() < length)
         {
            return null;
         }

         return sample.Average(it => (double) it.Close);
      }
   }
}
